package org.dromara.common.mail.config

import cn.hutool.extra.mail.MailAccount
import org.dromara.common.mail.config.properties.MailProperties
import org.springframework.boot.autoconfigure.AutoConfiguration
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean

/**
 * JavaMail 配置
 *
 * @author Michelle.Chung
 * @updater LikeYouDo
 * @date 2025/1/25 18:55
 */
@AutoConfiguration
@EnableConfigurationProperties(MailProperties::class)
class MailConfig {
    @Bean
    @ConditionalOnProperty(value = ["mail.enabled"], havingValue = "true")
    fun mailAccount(mailProperties: MailProperties): MailAccount {
        return MailAccount().apply {
            host = mailProperties.host
            port = mailProperties.port
            isAuth = mailProperties.auth
            from = mailProperties.from
            user = mailProperties.user
            pass = mailProperties.pass
            socketFactoryPort = mailProperties.port
            isStarttlsEnable = mailProperties.starttlsEnable
            isSslEnable = mailProperties.sslEnable
            setTimeout(mailProperties.timeout)
            setConnectionTimeout(mailProperties.connectionTimeout)
        }
    }
}
